<!-- 文件所有权归北京优博泰克 -->
<template>
    <div class="caseView" v-loading="loading">
        <dy-form :elForm_Options="elForm_Options" @bottonHandle="opbuttonclick" @openBack="openFunction"
            :flagIndex="flagIndex" ref="myForm" />
    </div>
</template>
<script setup>
import { ElMessageBox, ElMessage } from 'element-plus';
import { reactive, toRefs, ref, onMounted, nextTick } from 'vue';
import { useRouter, useRoute } from 'vue-router'
import commonV from '@/common/index'
import { routerConfig } from '@/store/router'
import api from '@/api'

const router = useRouter()
const route = useRoute()

let routerStore = routerConfig()

let props = defineProps({
    flagIndex: {
        type: String,
        default: () => 'detail'
    }
})

let state = reactive({
    loading: false,
    currentRow: null
})
let { loading } = toRefs(state)

//表单数据
let elForm_Options = reactive({
    pagetitle: '整车试验case库', //页面名称
    getwayname: "vehiclecase",//获取方法
    //操作button
    opbutton: [{
        label: "保存",
        dealway: "submitForm",
        icon: 'Checked',
        type: "primary",
        flagIndexList: ['edit', 'create'],
    }, {
        label: "编辑",
        dealway: "edit_btn",
        icon: 'Edit',
        flagIndexList: ['detail']
    },
    {
        label: "删除",
        dealway: "delete_btn",
        icon: 'Delete',
        type: "danger",
        plain: true,
        flagIndexList: ['detail']
    }],
    //表单循环
    wt_mesage: [{
        label: "case编号",
        prop: "case_no",
        paramtype: 'input',
        required: true,
        disabled: true
    }, {
        label: "标准号",
        prop: "standard_num",
        paramtype: 'input'
    }, {
        label: "标准名称",
        prop: "standard_name",
        paramtype: 'input'
    }, {
        label: "v1",
        prop: "v1",
        paramtype: 'input'
    },
    {
        label: "v2",
        prop: "v2",
        paramtype: 'input'
    }, {
        label: "v3",
        prop: "v3",
        paramtype: 'input'
    }, {
        label: "v4",
        prop: "v4",
        paramtype: 'input'
    }, {
        label: "是否委托",
        prop: "if_entrust",
        paramtype: 'select',
        options: 'boolint_status',
    },
    {
        label: "是否发生直接费用",
        prop: "if_direct_cost",
        paramtype: 'select',
        options: 'boolint_status',
    },
    {
        label: "收入合计",
        prop: "total_price",
        paramtype: 'input',
    }]
})

//实例
let myForm = ref(null)

onMounted(() => {
    if (props.flagIndex == 'create') {
        openFunction({})
        return
    }

    myForm.value.getFormData()
})

//打开页面执行
let openFunction = async (row) => {
    state.currentRow = JSON.parse(JSON.stringify(row))
    if (props.flagIndex == 'create') {
        state.currentRow.case_no = await commonV.creatnewcode("VC")
    }

    //表单数据赋值
    myForm.value.setFormData(state.currentRow)
}

//按钮事件
let opbuttonclick = (wayname) => {
    if (wayname == 'submitForm') {
        submitForm()
        return
    }

    if (wayname == 'returnPage') {
        returnPage()
        return
    }

    if (wayname == 'edit_btn') {
        edit_btn()
        return
    }

    if (wayname == 'delete_btn') {
        delete_btn()
        return
    }
}

//返回
let returnPage = () => {
    //删除tagViews
    routerStore.delMapTagHandle(route.path)
    if (['detail', 'create'].includes(props.flagIndex)) {
        //返回主页
        router.push({
            path: '/syStandardLib/vehicleTestLibrary'
        })
        return
    }
    router.push({
        path: '/syStandardLib/vehicleTestLibrary/' + route.params.id
    })
}

//编辑
let edit_btn = () => {
    router.push({
        path: '/syStandardLib/vehicleTestLibrary/edit/' + route.params.id
    })
}

//删除
let delete_btn = () => {
    ElMessageBox.confirm(
        '确定删除此条项目?',
        '提示',
        {
            confirmButtonText: '确 定',
            cancelButtonText: '取 消',
            type: 'warning',
        }
    ).then(async () => {
        state.loading = true
        await api.changeaxiosdata(elForm_Options.getwayname, 'delete', { id: route.params.id })
        ElMessage({
            message: '删除成功',
            type: 'success'
        })
        state.loading = false
        returnPage()
    }).catch(() => { })
}

//保存
let submitForm = () => {
    myForm.value.submitFormData()
}
</script>
<style scoped>
.borderleri {
    border: 1px solid #D3DCE6;
    border-top: none;
}
</style>
